﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YJKMenuAssist.Utility;

namespace YJKMenuAssist
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class MenuCollectionTool : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			string text = Path.Combine(Product.UserDataLocation, "CustomMenu.xml");
			if (!File.Exists(text))
			{
				File.Copy(Path.Combine(Product.DataLocation, "CustomMenu.xml"), text, true);
			}
			new XmlManager(text).ImportOpeningByXml(out MenuCollectionTool.m_CommandTableInfos, "Original");
			MenuManageForm menuManageForm = new MenuManageForm(MenuCollectionTool.m_CommandTableInfos, cmdData);
			this.SetFormParentHandle(menuManageForm);
			menuManageForm.ShowDialog();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void SetFormParentHandle(Window form)
		{
			new WindowInteropHelper(form).Owner = Process.GetCurrentProcess().MainWindowHandle;
		}

		internal static MainPage m_MainPage;

		internal static UIControlledApplication m_UICtrlApplication;

		internal static MenuManageApplication m_ThisApp;

		internal static CommandTableInfos m_CommandTableInfos;
	}
}
